<template>
  <section class="cms-body" v-loading="loading">
    <!-- 返回组件 -->
    <cms-back></cms-back>
    <el-form ref="form" :model="dataInfo" :rules="rules" class="cms-form" label-width="162px">


      <el-form-item label="分类Id" class="flex-50" prop="goodsTagId" v-if="isType('update')">
        <el-input v-model="dataInfo.goodsTagId" class="cms-width" :disabled="isType('update')"></el-input>
      </el-form-item>

      <el-form-item label="分类名称" class="flex-50" prop="tagName">
        <el-input v-model="dataInfo.tagName" class="cms-width"></el-input>
      </el-form-item>

      <el-form-item label="排序" class="flex-50" prop="goodsTypeSort">
        <el-input v-model="dataInfo.tagSort" class="cms-width"></el-input>
      </el-form-item>

      <el-form-item label="上架" class="flex-50" prop="goodsTagStatus">
        <el-radio-group v-model="dataInfo.goodsTagStatus">
          <el-radio :label="1">上架</el-radio>
          <el-radio :label="0">不上架</el-radio>
        </el-radio-group>
      </el-form-item>


      <el-form-item label="分类图标" class="flex-50" prop="tagIcon">
        <cms-upload :src="dataInfo.tagIcon" @change="getTagIcon" :is-mark="false" uploadPath="images"></cms-upload>
        <span style="color:red">建议上传图片尺寸 96*96</span>
      </el-form-item>

    </el-form>

    <div class="form-footer">
      <el-button type="warning" v-if="isType('save')" @click="add(true)" v-perms="'/goodsTag/save'">提交并继续添加</el-button>
      <el-button type="primary" v-if="isType('save')" @click="add(false)" v-perms="'/goodsTag/save'">添加</el-button>
      <el-button type="primary" v-if="isType('update')" @click="update()" v-perms="'/goodsTag/update'">修改</el-button>
      <el-button type="info" @click="$reset">重置</el-button>
      <span class="gray" v-if="isType('save')">选择“提交并继续添加”按钮会停留在添加页面；选择“提交”按钮后将跳转到对应的列表页</span>
    </div>

  </section>
</template>
<script>
  import axios from "axios/index";
  import va from "@/rules";
  import formMixns from "@/mixins/form";

  export default {
    mixins: [formMixns],
    data() {
      let required = va.required();
      let number = va.number();
      return {
        rules: {//校验规则
          id: [number],
          name: [required],
          tagSort: [required, number]
        },
        dataInfo: {
          goodsTagStatus:0,
          goodsTagId: '',
          tagName: '',
          tagIcon: '',
          tagSort: ''
        }
      };
    },
    methods: {
      getDataInfo(id, type) {
        if (type == 'update') {
          //重写获取表单数据
          let api = this.$api; //API地址
          axios.all([
            axios.post(api.goodsTagGet, {goodsTagId: id}),
          ]).then(axios.spread((res) => {
            console.log(res);
            this.loading = false;
            let dataInfo = res.body; //将用户数据复制给dataInfo
            dataInfo.goodsTagStatus = Number(dataInfo.goodsTagStatus);
            this.dataInfo = dataInfo;
            this.$refs["form"].resetFields();
          })).catch(error => {
            console.log(error);
          });
        }
        this.loading = false;
      },
      add(state) {
        this.saveDataInfo(state, this.$api.goodsTagSave, this.dataInfo, "pageList");
      },
      update() {
        console.log(this.dataInfo);
        this.updateDataInfo(this.$api.goodsTagUpdate, this.dataInfo, "pageList");
      },
      getTagIcon(url, index, pIndex) {
        this.dataInfo.tagIcon = url;
      },
      handleClose(done) {
        done();
      }
    },
    created() {
      //初始获取数据
      this.getDataInfo(this.id, this.type);
    }
  };
</script>

<style>
</style>
